package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderSettingService;
import com.atguigu.utils.POIUtils;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/OrderSetting")
public class OrderSettingController {
    @Reference
    OrderSettingService orderSettingService;
@RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
    try {
        //使用poi工具类解析excel文件，读取里面的内容，String[]代表一条数据
        List<String[]> list = POIUtils.readExcel(excelFile);
        //把List<String[]>数据转换成List<OrderSetting>数据
        List<OrderSetting> listData = new ArrayList<OrderSetting>();
        //迭代里面的每一行数据，进行封装到集合里面
        for (String[] strArray : list) {
            //获取到一行里面，每个表格的数据，进行封装
            String date = strArray[0];
            String number = strArray[1];
            //构建一个实体类
            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setOrderDate(new Date(date));
            orderSetting.setNumber(Integer.parseInt(number));
            //预约的默认人数为0
            orderSetting.setReservations(0);
            //
            listData.add(orderSetting);

        }
        orderSettingService.batchsave(listData);
        return new Result(true, MessageConstant.UPLOAD_SUCCESS);
    } catch (Exception e) {
        e.printStackTrace();
        return new Result(false,MessageConstant.UPLOAD_FAIL);
    }

}

}
